#include "config.h"

#include "ms2lstat.h"

ecm_hashtable_t	hash_client;
static ecm_ptrs_t	*ptrs_client;

static client_t *
create_client(const char *idstr)
{
	client_t	*client;

	client = (client_t *)ecm_malloc(sizeof(client_t));
	client->cid = ecm_ptrs_add(ptrs_client, client);
	client->idstr = ecm_strdup(idstr);

	ecm_hashtable_str_insert(hash_client, client, idstr);

	return client;
}

ecm_uint32
get_client_id(const char *idstr)
{
	client_t	*client;

	client = (client_t *)ecm_hashtable_str_get(hash_client, idstr);
	if (client == NULL) {
		client = create_client(idstr);
	}
	return client->cid;
}

client_t *
get_client(ecm_uint32 cid)
{
	return (client_t *)ecm_ptrs_get(ptrs_client, cid);
}

static void
report_all_clients(FILE *fp_client)
{
	client_t	*client;
	ecm_uint32	i;

	ecm_fprintf(fp_client, "# mid length size uri\n");
	for (i = 0; i < ptrs_client->len; i++) {
		client = (client_t *)ptrs_client->pdata[i];
		ecm_fprintf(fp_client, "%6u %s\n", i + 1, client->idstr);
	}
}

void
report_client_result(FILE *fp)
{
	FILE	*fp_client = setup_logging("client");

	ecm_fprintf(fp, "Total Number of Clients:            %u\n", ecm_hashtable_getsize(hash_client));

	report_all_clients(fp_client);
	ecm_fclose(fp_client);
}

ECMBOOL
init_client(void)
{
	hash_client = ecm_hashtable_create(256);
	ptrs_client = ecm_ptrs_new(256);

	return ECM_TRUE;
}
